var wxCharts = require('../../utils/wxcharts-min.js');
var app = getApp();
var radarChart = null;
Page({
  data: {
    tupian: getApp().globalData.imgUrl,
  },
  touchHandler: function (e) {
    console.log(radarChart.getCurrentDataIndex(e));
  },
  onLoad: function (e) {
    wx.showLoading({
      title: '加载数据中...',
      mask: true,
    });
    var That = this;
    let apiurl = 'userInfo/getInfo';
    console.log(apiurl);
    app.getdata.fetchApi(apiurl, {})
      .then(d => {
        if (d.status == 1) {
          console.log(d.data.canvas)
          var windowWidth = 320;
          try {
            var res = wx.getSystemInfoSync();
            windowWidth = res.windowWidth;
          } catch (e) {
            console.error('getSystemInfoSync failed!');
          }
          //
          for (let i = 0; i < d.data.canvas.length; i++) {
            console.log(i);
          }
          radarChart = new wxCharts({
            canvasId: 'radarCanvas',
            type: 'radar',
            // categories: ['文科', '流行', '娱乐', '生活', '理科', '文艺'],
            categories: d.data.canvas.names,
            series: [{
              name: d.data.grad_name,
              color: "#8e86c3",
              data: d.data.canvas.data
              // data: [0,0,0,0,0,0,]
            }],
            width: windowWidth,
            height: 260,
            extra: {
              legendTextColor: "#8e86c3",
              radar: {
                max: 200,
                labelColor: "#8e86c3",
              }
            }
          }); 
          this.setData({ list: d.data.canvas})
        } else {
          this.setData({ hasMore: false, loading: false })
        }

      })
      .catch(e => {
        this.setData({ subtitle: '获取数据异常', loading: false })
        console.error(e)
      })
  },
  savePhoto(){//保存相册
    app.showToast({title:'请稍后',icon:'loading'});
    wx.canvasToTempFilePath({
      canvasId: 'radarCanvas',
      width: 750,
      height: 520,
      destWidth: 750,
      destHeight: 520,
      success: function(res) {
        console.log(res.tempFilePath)
        wx.uploadFile({
          url: app.globalData.siteBaseUrl+'/user/user_poster', //仅为示例，非真实的接口地址
          filePath: res.tempFilePath,
          name: 'download',
          formData:{
            'session_key': wx.getStorageSync('session_key')
          },
          header: { "Content-Type": "multipart/form-data"},
          success: function(res){
            let result = JSON.parse(res.data);
            let path = app.globalData.siteUrl+result.data.poster_url;
            console.log(path)

            wx.downloadFile({
              url: path, //服务器资源地址
              success: function(res) {
                if (res.statusCode === 200) {
                  wx.saveImageToPhotosAlbum({
                      filePath: res.tempFilePath,
                      success(res) {
                          //console.log(res)
                          app.showModal({content:'已保存到相册，你可以进行分享了。'});
                      },
                      fail(res) {
                        console.log(res)
                        app.showToast({title:'保存失败'});
                      },
                  });
                }
              }
            })
            //
          }
        })
      } 
    });

  },
  onShareAppMessage: function (res) {//分享

    let that = this;
    let user_info = app.getUserInfo();
    let _path = '/pages/shouye/shouye';
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res)
      let stype = res.target.dataset.type;//分享类型
      if (stype == 1) {
        wx.showShareMenu({
          withShareTicket: true
        });//分享前置code

        console.log(_path)
        return {
          title: user_info.display_name + ' 向你发起知识挑战，看谁知道面更广！',
          path: _path,
          success: function (res) {
            // 转发成功
            if (res.shareTickets) {
              wx.getShareInfo({// 获取转发详细信息
                shareTicket: res.shareTickets[0],
                success(res) {
                  console.log(res.encryptedData)


                  let apiurl = 'user/share_points';//分享获取金币
                  let params = { is_group: 1, share_type: 'share_user_center', encryptedData: res.encryptedData, iv: res.iv };
                  console.log(params);
                  app.getdata.fetchApi(apiurl, params)
                    .then(d => {
                      if (d.status == 1) {
                        app.showToast({ title: '获取金币' + d.data.points });
                      } else {
                        app.showModal({ content: d.message }); return;
                      }
                    })
                    .catch(e => {
                      this.setData({ subtitle: '获取数据异常', loading: false })
                    })
                },
                fail() { },
                complete() { }
              });
            } else {
              if (!res.encryptedData) {
                app.showModal({ content: '分享到群才有奖励哦' }); return;
              }
            }//分享end

          },
          fail: function (res) {
            // 转发失败
          }
        }
      } else {

      }

    }

  },
  paiwei: function (e) {
    //e.currentTarget.dataset
    app.turnToPage('/pages/paiwei/paiwei');

  },
});


